<?php

class TransactionController extends PsController
{

	public $layout = 'manage';

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'actions' => array('index'),
						'roles' => array('ManageAgency.SystemSettings.Manage'),
					),
						), parent::accessRules());
	}

	public function actionIndex()
	{
		$agency = $this->loadAgency();
		$agencySetting = array();

		if ($agency->settings == null) {
			$agency->settings = array();
		} else {
			$types = array(
				'Sale' => 'Seller',
				'Purchase' => 'Buyer',
				'Lease by Landlord' => 'Landlord',
				'Lease by Tenant' => 'Tenant',
			);
			$settings = $agency->getSettings();
			foreach ($types as $key => $type) {
				$token = 'PropertyCase_' . str_replace(' ', '', $key) . '_P';
				$k = $token . 1;
				$agencySetting[$k] = $settings[$k];
				$k .= '_type';
				$agencySetting[$k] = $settings[$k];
				$k = $token . 2;
				$agencySetting[$k] = $settings[$k];
				$k .= '_type';
				$agencySetting[$k] = $settings[$k];
			}
		}

		if (!empty($_POST['AgencySetting'])) {
			foreach ($_POST['AgencySetting'] as $key => $value) {
				$agency->setSetting($key, $value);
			}

			if ($agency->save(true, array('settings'))) {
				PsSystemMessage::add('Your changes have been saved.', 'success');
				$this->refresh();
			}
		}

		$data = array();
		$data['agency'] = $agency;
		$data['AgencySetting'] = $agencySetting;
		$this->render('index', $data);
	}

	public function loadAgency()
	{
		return Yii::app()->user->model->agency;
	}

}

?>
